package cn.smile.business.basic.impl;

import cn.smile.bean.dto.basic.DayDataCountDTO;
import cn.smile.business.basic.*;
import cn.smile.utils.DateFormatUtil;
import com.alibaba.fastjson2.JSON;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@Service
public class DataCountServiceImpl implements IDataCountService {
    @Resource
    private IMemberService memberService;
    @Resource
    private ITopUpBillService topUpBillService;
    @Resource
    private IExpenseBillService expenseBillService;
    @Resource
    private IVoucherMemberService vouMenService;

    @Override
    public DayDataCountDTO countShopDataByDay(String date) {
        log.info("[DataCountServiceImpl].[countShopDataByDay] ------> 统计指定日期店铺数据 Start, date = {}", date);
        String queryTimeStart = String.format("%s 00:00:00", date);
        String queryTimeEnd = String.format("%s 23:59:59", date);
        //统计数据
        DayDataCountDTO dto = getShopDataCountDTO(queryTimeEnd, queryTimeStart);
        log.info("[DataCountServiceImpl].[countShopDataByDay] ------> 统计指定日期店铺数据 End, dto = {}", JSON.toJSONString(dto));
        return dto;
    }

    @Override
    public DayDataCountDTO countShopDataByMonth(String date) {
        log.info("[DataCountServiceImpl].[countShopDataByMonth] ------> 统计指定月份店铺数据 Start, date = {}", date);
        String queryTimeStart = String.format("%s-01 00:00:00", date);
        Date startDay = DateFormatUtil.parseStrToDate(queryTimeStart, DateFormatUtil.DATE_TIME_FORMAT_1);
        String queryTimeEnd = DateFormatUtil.parseDateToStr(DateFormatUtil.getLastDayOfMonth(startDay), DateFormatUtil.TO_DATE_END);

        //统计数据
        DayDataCountDTO dto = getShopDataCountDTO(queryTimeEnd, queryTimeStart);
        log.info("[DataCountServiceImpl].[countShopDataByMonth] ------> 统计指定月份店铺数据 End, dto = {}", JSON.toJSONString(dto));
        return dto;
    }

    /**
     * 店铺数据统计
     *
     * @param queryTimeEnd   统计开始时间
     * @param queryTimeStart 统计结束时间
     * @return 统计结果
     */
    private DayDataCountDTO getShopDataCountDTO(String queryTimeEnd, String queryTimeStart) {
        DayDataCountDTO dto = new DayDataCountDTO();
        dto.setAllMemberNum(memberService.countMemberByShop(queryTimeEnd));
        dto.setAddMemberNum(memberService.countAddMemberNum(queryTimeStart, queryTimeEnd));
        dto.setContinueMemberNum(topUpBillService.countContinueMemberNum(queryTimeStart, queryTimeEnd));
        //会员充值金额
        BigDecimal topUpAmount = topUpBillService.countTopUpAmount(queryTimeStart, queryTimeEnd);
        dto.setTopUpAmount(topUpAmount);
        //非会员消费金额
        BigDecimal nonmemberExpendAmount = expenseBillService.countExpenseAmount(queryTimeStart, queryTimeEnd, Boolean.FALSE);
        dto.setNonmemberExpendAmount(nonmemberExpendAmount);
        //会员消费金额
        BigDecimal memberExpendAmount = expenseBillService.countExpenseAmount(queryTimeStart, queryTimeEnd, Boolean.TRUE);
        dto.setMemberExpendAmount(memberExpendAmount);
        //入账金额
        dto.setTurnover(topUpAmount.add(nonmemberExpendAmount));
        //赠送金额
        dto.setMemberGiftExpendAmount(expenseBillService.countGiftExpenseAmount(queryTimeStart, queryTimeEnd));
        //统计订单数量
        Long unMemberOrder = expenseBillService.countOrderNum(queryTimeStart, queryTimeEnd, Boolean.FALSE);
        dto.setUnMemberOrderNum(unMemberOrder);
        Long memberOrder = expenseBillService.countOrderNum(queryTimeStart, queryTimeEnd, Boolean.TRUE);
        dto.setMemberOrderNum(memberOrder);
        dto.setAllOrderNum(unMemberOrder + memberOrder);
        //统计会员代金券发放数量
        dto.setSendVoucherNum(vouMenService.countSendVouMemNum(queryTimeStart, queryTimeEnd));
        //统计会员代金券使用数量
        dto.setUseVoucherNum(vouMenService.countUseVouMemNum(queryTimeStart, queryTimeEnd));
        return dto;
    }
}